var netcore = require('../../../utils/network.js')
var tripleDes = require('../../../utils/CKCrypto.js')
var course = require('../../index/model/CourseIndex.js')
var comm = require('../../../utils/comm_util.js')
//获取钱包信息
function getWalletInfo(callBack){
  var map=new Map()
  netcore.requestPost("wallet/getWalletBalance.do", map, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: ''
    })
  })
}
function getXiaoFeiList(pageIndex,callBack){
  getOrderList("4", pageIndex, callBack)
}
function getCongZhiList(pageIndex,callBack){
  getOrderList("3", pageIndex, callBack)
}
function getOrderList(type, pageIndex,callBack){
  var map = new Map()
  map.set("orderPayType", type)
  map.set("currentPage", pageIndex);
  map.set("pageSize", "10");
  netcore.requestPost("wallet/getWalletList.do", map, function (success) {
    success.forEach(function(item,index){
      // Date.parse(item.orderPayTime)
      item.orderPayTime = comm.formatDate(item.orderCreateTime)
    })
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon: 'none'
    })
  })
}
function recharge(money,callBack){
  var map = new Map()
  map.set("amount", money);
  map.set("payChannel", "1012");
  map.set("payType", "1001");
  map.set("openid", tripleDes.enCode(getApp().globalData.openId))
  map.set("lat", getApp().globalData.userLocationInfo.lat)
  map.set("lng", getApp().globalData.userLocationInfo.lng)
  map.set("origin", "xcs")
  netcore.requestPost("wx/pay.do", map, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon:'none',
      duration: 3000
    })
  })
}
function withdrawal(money,openId,callBack){
  var map=new Map()
  map.set("amount", money)
  map.set("openid", tripleDes.enCode(openId))
  map.set("lat", getApp().globalData.userLocationInfo.lat)
  map.set("lng", getApp().globalData.userLocationInfo.lng)
  map.set("origin", "xcs")
  netcore.requestPost("wx/transfersCash.do", map, function (success) {
    callBack(success)
  }, function (code, errMsg) {
    wx.showToast({
      title: errMsg,
      icon:'none',
      duration: 3000
    })
  })
}
function getOpenId(callBack){
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      if (res.code) {
        console.log(res)
        var map = new Map()
        map.set("code", res.code)
        map.set("origin","xcx")
        netcore.requestPost("appshare/getMinOpenId.do", map, function (data) {
          callBack(data.openid)
        }, function (error) {
          callBack(null)
        })
      }
    }
  })
}
module.exports = {
  getWalletInfo: getWalletInfo,
  getXiaoFeiList: getXiaoFeiList,
  getCongZhiList: getCongZhiList,
  recharge: recharge,
  withdrawal: withdrawal,
  getOpenId: getOpenId
}